/*
 * Created on Sep 28, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package schoolsource.model;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;

import schoolsource.sql.SchoolDBSQL;
import schoolsource.util.SchoolLogger;

/**
 * 
 * @author Steven Steele
 * Copyright 2003
 *
 * Contains information for courses.
 */
public class CourseContainer extends Container {

	// The map of timer buckets for all databases
	protected static Hashtable dbStores = new Hashtable(); 
	// The auto-generated id of the course
	private IntegerItem courseAssignedID =
		new IntegerItem(SchoolDBSQL.COURSEASSIGNEDID, "", true);
	// A short name for the course
	private StringItem courseShortName =
		new StringItem(SchoolDBSQL.COURSESHORTNAME, "", false);
	// A longer description of the course
	private StringItem courseDescription =
		new StringItem(SchoolDBSQL.COURSEDESCRIPTION, "", false);
	// The ID of the course assigned by the school.
	private StringItem courseSchoolID =
		new StringItem(SchoolDBSQL.COURSESCHOOLID, "", false);

	// Default constructor
	private CourseContainer() {

	}

	/**
	 * Constructor creates a course container with values loaded from the database 
	 * using the auto-generated ID passed in as a parameter.
	 * @param id The auto-generated ID of the course to load.
	 */
	private CourseContainer(String id, String dbName) {
		getCourseAssignedID().setValue(id);
		SchoolDBSQL.getSchoolDBSQL(dbName).loadCourse(
			getValidationItems(),
			getCourseAssignedID());
	}

	public static CourseContainer getCourseContainer(String cID, String dbName) {
		CourseContainer returnVal = null;
		DBStore dbs = null;
		
		if (cID == "-1") {
			returnVal = new CourseContainer();
		}
		else {
			if ((dbs = (DBStore) dbStores.get(dbName)) == null) {
				dbs = new DBStore();
				dbStores.put(dbName, dbs);
				dbs.start();
			}
			if ((returnVal = (CourseContainer) dbs.getContainer(cID)) == null) {
				returnVal = new CourseContainer();
				returnVal.getCourseAssignedID().setValue(cID);
				SchoolDBSQL.getSchoolDBSQL(dbName).loadCourse(returnVal.getValidationItems(), returnVal.getCourseAssignedID());
				dbs.addContainer(returnVal, cID);
			}
		}

		return returnVal;
	}

	/* (non-Javadoc)
	 * @see schoolsource.model.Container#getValidationItems()
	 */
	public Item[] getValidationItems() {
		ArrayList al = new ArrayList();
		al.add(courseAssignedID);
		al.add(courseShortName);
		al.add(courseDescription);
		al.add(courseSchoolID);

		return (Item[]) al.toArray(new Item[al.size()]);
	}

	/**
	 * Getter for the course assigned ID.
	 * @return The auto-generated ID of the course.
	 */
	public IntegerItem getCourseAssignedID() {
		return courseAssignedID;
	}

	/**
	 * Getter for the course description.
	 * @return A detailed description of the course.
	 */
	public StringItem getCourseDescription() {
		return courseDescription;
	}

	/**
	 * Getter for the course school-assigned ID.
	 * @return The ID assigned to the course by the school.
	 */
	public StringItem getCourseSchoolID() {
		return courseSchoolID;
	}

	/**
	 * Getter for the short name of the course.
	 * @return The short name of the course.
	 */
	public StringItem getCourseShortName() {
		return courseShortName;
	}

	/**
	 * Prints the values of the container.
	 */
	public void printItems() {
		SchoolLogger.logger.log(
			Level.INFO,
			"************************************************");
		Item iList[] = getValidationItems();
		for (int j = 0; j < iList.length; j++) {
			if (iList[j] != null) {
				SchoolLogger.logger.log(
					Level.INFO,
					iList[j].getFieldName() + ": " + iList[j].toString());
			}
		}
	}

}
